const query = require('./fun')
const sqlCount = 'SQL_CALC_FOUND_ROWS';
const { USER, SWIPER, SPEED, ORDERS } = require('./database')
let { sqlUpdateStr, sqlAddStr, sqlSelectStr, sqlWhereStr } = require('../util.js');
/**
 * 判断是否存在用户
 */
function isUser(data) {
    return new Promise((resolve, reject) => {
        query(`select  * from ${USER} where openid=?`, data)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                console.log(err)
                reject(err)
            })
    })
}
/**
 * 添加用户
 */
function addUser(data) {
    return new Promise((resolve, reject) => {
        query(`INSERT INTO ${USER} (userId,openid) VALUES(null,?) `, data)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 修改用户信息
 */
function updataUser(userId, data) {
    return new Promise((resolve, reject) => {
        let sql = '';
        Object.entries(data).forEach(item => {
            if (sql.toString()) {
                sql += ',';
            }
            sql += `${item[0]}='${item[1]}'`
        })
        query(`UPDATE ${USER} SET ${sql} WHERE userId=${userId}`)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 根据用户的id返回用户信息
 */
function GetUsersById(id) {
    return new Promise((resolve, reject) => {
        query(`select userName,userUrl,userSex,userId,beginMoney from ${USER} where userId=?`, { id })
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 获取用户余额
 */
function getbeginMoneyUser(userId) {
    return new Promise((resolve, reject) => {
        query(`select beginMoney from ${USER} where userId=?`, { userId })
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(reject)
            })
    })
}
/**
 * 用户充值
 */
function userRecharge(userId, data) {
    return new Promise((resolve, reject) => {
        let sql = `beginMoney=beginMoney+${data.Money}`;
        query(`UPDATE ${USER} SET ${sql} WHERE userId=${userId}`)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(reject)
            })
    })
}
/**
 * 获取所有轮播图
 */
function getSwiperList() {
    return new Promise((resolve, reject) => {
        query(`select *  from ${SWIPER} ORDER BY swiperIndex ASC`)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 获取高铁列表
 */
function getSpeedList(like) {
    return new Promise((resolve, reject) => {
        let condition = '';
        condition = sqlSelectStr(condition, like);
        console.log(like)
        query(`select  ${sqlCount} * from ${SPEED} ${condition} `, {}, true)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 修改高铁
 */
function updataSpeed(speedId, data) {
    return new Promise((resolve, reject) => {
        let sql = '';
        sql = sqlUpdateStr(sql, data);
        query(`UPDATE ${SPEED} SET ${sql} WHERE speedId=${speedId}`)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 添加订单
 */
function addOrder(data) {
    return new Promise((resolve, reject) => {
        let obj = sqlAddStr(data);
        query(`INSERT INTO ${ORDERS}(${obj.keys}) VALUES(${obj.vals}) `)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 获取订单
 */
function getOrder(num, limit, like) {
    return new Promise((resolve, reject) => {
        let condition = '';
        condition = sqlWhereStr(condition, {
            [`${ORDERS}.userId`]: like.userId,
            [`${ORDERS}.speedId`]: `${SPEED}.speedId`
        });
        query(`select  ${sqlCount} ${ORDERS}.*,${SPEED}.* from ${ORDERS},${SPEED} ${condition}  limit ?,${limit}`, { num }, true)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 删除订单
 */
function delOrder(orderId) {
    return new Promise((resolve, reject) => {
        query(`DELETE FROM ${ORDERS} WHERE orderId=?`, { orderId })
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 获取订单
 */
function getOrderById(orderId) {
    return new Promise((resolve, reject) => {
        query(`select ${ORDERS}.*,${SPEED}.* from ${ORDERS},${SPEED} where ${ORDERS}.orderId=? and ${ORDERS}.speedId=${SPEED}.speedId`, { orderId })
            .then((res) => {
                resolve(res[0])
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 修改订单
 */
function updataOrder(orderId, data) {
    return new Promise((resolve, reject) => {
        let sql = '';
        sql = sqlUpdateStr(sql, data);
        query(`UPDATE ${ORDERS} SET ${sql} WHERE orderId=${orderId}`)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
exports.isUser = isUser;
exports.addUser = addUser;
exports.updataUser = updataUser;
exports.GetUsersById = GetUsersById;
exports.getSwiperList = getSwiperList;
exports.getbeginMoneyUser = getbeginMoneyUser
exports.userRecharge = userRecharge
exports.getSpeedList = getSpeedList;
exports.updataSpeed = updataSpeed;
exports.addOrder = addOrder;
exports.getOrder = getOrder;
exports.delOrder = delOrder;
exports.getOrderById = getOrderById;
exports.updataOrder = updataOrder;